import 'package:flutter/material.dart';
import 'package:flutter_sound/public/flutter_sound_player.dart';
import 'package:flutter_sound/public/flutter_sound_recorder.dart';
import 'package:flutter_weixin/app/modules/wechat_content/views/wechat_content_view.dart';
import 'package:flutter_weixin/app/widgets/panel.dart';
import 'package:get/get.dart';
import 'package:web_socket_channel/io.dart';

class WechatContentController extends GetxController
    with GetTickerProviderStateMixin {

  String appid = "62b8249b";

  String appkey = "eefcaf028bfb71b9af6733dd9f3cc6ad";

  IOWebSocketChannel? channel;

  RxString currentTs = "".obs;

  String getTs(){
    return (DateTime.now().millisecondsSinceEpoch ~/ 1000).toString();
  }



  RxString mPath = "".obs;


  RxDouble width = 0.0.obs;
  RxDouble left = 0.0.obs;
  Rx<Color> color = Rx<Color>(Color(0xFFa7e678));

  RxDouble voiceHeight = 0.0.obs;

  RxDouble voicePositioned = 200.0.obs;
  RxDouble voiceFrameHeight = 44.0.obs;

  Offset? startOffset;

  // 是否在长按
  RxBool isLongPress = false.obs;

  // 长按状态 0 说话中 1 删除  2 转换成文字
  RxInt longPressState = 0.obs;

  RxList<Message> messageList = RxList([]);

  PanelController panelController = PanelController();

  RxBool inputState = false.obs;

  // 0 默认 1 表情 2 添加 3 声音
  RxInt state = 0.obs;

  List<String> list = ["表情", "喜欢"];

  late TabController tabController;

  FocusNode commentFocus = FocusNode();

  Rx<TextEditingController> contentController = Rx(TextEditingController());

  // 当前选中的
  RxInt tabIndex = 0.obs;

  FlutterSoundPlayer myPlayer = FlutterSoundPlayer();
  FlutterSoundRecorder myRecorder = FlutterSoundRecorder();

  RxBool mPlayerIsInited = false.obs;
  RxBool myRecorderIsInited = false.obs;

  @override
  void onInit() {
    super.onInit();

    // openAudioSession
    myPlayer.openAudioSession().then((value) {
      mPlayerIsInited.value = true;
    });

    myRecorder.openAudioSession().then((value) {
      myRecorderIsInited.value = true;
    });

    width.value = (Get.width / 2);
    left.value = (Get.width / 2) / 2;
    tabController =
        TabController(length: list.length, vsync: this, initialIndex: 0);
    contentController.value.addListener(() {
      var text = contentController.value.text;
      if (text.isNotEmpty && !inputState.value) {
        inputState.value = true;
      }
      if (text.isEmpty) {
        inputState.value = false;
      }
    });

    tabController.addListener(() {
      tabIndex.value = tabController.index;
    });

    messageList.value = [];
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    myPlayer.closeAudioSession();
    myRecorder.closeAudioSession();
    tabController.dispose();
  }
}
